//
//  ZTLinkageData.m
//  YSSmartHome
//
//  Created by Yahaong on 16/9/6.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "ZTLinkageData.h"

@implementation ZTLinkageData
- (NSData *)toData
{
    char*  payload  = NULL;
    NSInteger dataLength = self.length + 16;
    payload =  malloc(dataLength);
    memset(payload,0, dataLength);
    uint32_t apsn = self.apsn;
    apsn = CFSwapInt32(apsn);
    memcpy(payload, &apsn, sizeof(apsn));
    uint64_t luid = self.luid;
    luid = CFSwapInt64(luid);
    memcpy(payload+4, &luid, sizeof(luid));
    uint8_t scid = self.scid;
    memcpy(payload+4+8, &scid, 1);
    uint8_t type = self.type;
    memcpy(payload+4+8+1, &type, 1);
    uint16_t length = CFSwapInt16(self.length);
    memcpy(payload+4+8+1+1, &length, 2);
    NSData *data =  [[NSData alloc]initWithBytes:payload length:16];
    NSMutableData *dataM =  [[NSMutableData alloc]initWithData:data];
    [dataM appendData:self.data];
    free(payload);
    return [dataM copy];
}

@end
